<?php
class Page extends Controller
{
	public function __construct()
	{
		parent::__construct();
		$catalogs = CatalogModel::getList(0);
		
		$configs = ConfigModel::getList();
		
		$this->assign('config', $configs);
		
		$this->assign('catalogs', $catalogs);
	}
	/**
	 * @param
	 */
	public function Archives()
	{
		$aid = isset($_REQUEST['aid']) ? intval($_REQUEST['aid']) : exit('lose param');
		$archive = ArchivesModel::get($aid);
		
		$this->hot(10);
		$this->hotByCat($archive['cid'], 10);
		$this->locat($archive['cid']);
		$this->assign('archive', $archive);
		$this->display('archives.tpl');
	}
	
	public function index()
	{
		$archives = array();
		$cats = CatalogModel::getList();
		foreach ($cats as $cat){
			$archives[$cat['id']] = ArchivesModel::getList("cid={$cat['id']}", 1, 30);
		}
		$this->assign('archives', $archives);
		$this->display('index.tpl');
	}
	
	public function catalog()
	{
		$cid = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : exit('lose param');
		$page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
		$pageSize = isset($_REQUEST['pageSize']) ? intval($_REQUEST['pageSize']) : 30;
		$where = 'cid=' . $cid;
		$archives = ArchivesModel::getList($where, $page, $pageSize);
		$count = ArchivesModel::getCount($where);
		$pageBreak = Util::page($count, $pageSize);
		
		$this->hot(10);
		$this->hotByCat($cid, 10);
		$this->assign('archives', $archives);
		$this->assign('pageBreak', $pageBreak);
		$this->display('catalog.tpl');
	}
	
	public function channel()
	{
		$cid = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : exit('lose param');
		$cats = CatalogModel::getList($cid);
		foreach ($cats as $cat){
			$archives[$cat['id']] = ArchivesModel::getList("cid={$cat['id']}", 1, 30);
		}
		
		$this->assign('archives', $archives);
		$this->display('channel.tpl');
	}
	
	public function hot($count=10)
	{
		$hots = ArchivesModel::getHot($count);
		$this->assign('hots', $hots);
	}
	
	public function hotByCat($cid, $count=10)
	{
		$hots = ArchivesModel::getHotByCat($cid, $count);
		$this->assign('catHots', $hots);
	}
	
	public function archivesByCat($cid, $page=1, $count=30)
	{
		$where = "cid={$cid}";
		$news = ArchivesModel::getList($where, $page, $count);
		$this->assign('news', $news);
	}
	
	private function locat($cid)
	{
		$cats = CatalogModel::getList(0);
		$locat = array();
		foreach ($cats as $item){
			if ($item['id']==$cid) array_push($locat, $item);
		}
		
		//向前找
		$tmp = $item;
		while($tmp['pid']){
			foreach ($cats as $pre){
				if ($tmp['pid']==$pre['id']){
					$tmp = $pre;
					array_unshift($locat, $pre);
				}
			}
		}
		
		//向后找
		$tmp = $item;
		$flag = true;
		while ($flag){
			$flag = false;
			foreach ($cats as $next){
				if ($tmp['id'] == $next['pid']){
					$flag = true; 
					$tmp = $next;
					array_push($locat, $tmp);
				}
			}
		}
		$this->assign('locat', $locat);
	}
}